<? 
	session_start();
	include ('conn.php');
	$db = get_conn();

  if ($_POST['Agregar']!=""){
  	$sql = "INSERT INTO rol (name) VALUES ('".$_POST['name']."')";
	$result = mysql_query($sql);
	if($result){
		$id_rol = mysql_insert_id();
		set_modulos($id_rol,$_POST['id_modulo']);
		do_redirect('../rol.php');	
	}else{
		
		error_handling(__FUNCTION__.$sql,mysql_error());
	}				  
  }//Agregar
  
  
  if ($_POST['Actualizar']!=""){
	$sql = "update rol 	set name='".$_POST['name']."' where id_rol=".$_POST['id_rol'];
	$result = mysql_query($sql);
	if($result){
		set_modulos($_POST['id_rol'],$_POST['id_modulo']);	
		do_redirect('../rol.php');	
	}else{
		
		error_handling(__FUNCTION__.$sql,mysql_error());
	}				  
  }//update
  
  
  if ($_POST['Eliminar']!=""){
	$sql = "delete from rol where id_rol=".$_POST['id_rol'];
	$result = mysql_query($sql);
	if($result){
		do_redirect('../rol.php');	
	}else{
		error_handling(__FUNCTION__.$sql,mysql_error());
	}				  
  }//delete
  
  if ($_POST['Nuevo']!=""){
		do_redirect('../rol.php');	
  }//nuevo 
  
////////////////////////////////////////////////////
function set_modulos($id_rol,$id_modulo){
	$sql="delete from rol_modulo where id_rol=".$id_rol;
	$result = mysql_query($sql);
	if(!$result){
		error_handling(__FUNCTION__.$sql,mysql_error());
	}
	for($i=0;$i < count($id_modulo);$i++){
		$sql="INSERT INTO rol_modulo (id_rol, id_modulo) VALUES (".$id_rol.", ".$id_modulo[$i].")";
		$result = mysql_query($sql);
		if(!$result){
			error_handling(__FUNCTION__.$sql,mysql_error());
		}		
	}
}




////////////////////////////////////////////////////
?>
  
